from common.sqlalchemy_package import *
from flask import g


class Place(BaseModel):
    __tablename__ = 'place'
    place_id = mapped_column(Integer, primary_key=True)  # 车位id
    place_number=mapped_column(Integer) #车位标识
    car_number=mapped_column(String) #车牌号
    car_state=mapped_column(Integer) #车辆状态(0表示故障,1表示正常)
    place_state=mapped_column(String) #车位状态(True表示开放,False表示未开放)

    # 根据车位id查询该条信息
    @staticmethod
    def query_place_id_first(place_id):
        query = g.db_session.query(Place).filter(Place.place_id == int(place_id)).first()
        return query

    #根据车牌号查询车位标识
    @staticmethod
    def query_plate_car_number(car_number):
        query=g.db_session.query(Place).filter(Place.car_number==car_number).first()
        if query is not None:
            return query.place_number #返回该车牌号对应的车位标识
        else:
            return -1

    #查询剩余车位以及车位开放状态,返回剩余车位
    @staticmethod
    def query_place_state():
        query=g.db_session.query(Place).filter(Place.car_number==None,Place.place_state=='True').all()
        if query is None or query==[]:
            return -1 #表示已无车位
        else:
            place=[]
            for i in query:
                place.append(i.place_number)
        return place #返回车位数,列表


    #根据已有的车位修改为车辆信息
    @staticmethod
    def modify_place(place_number,car_number):
        try:
            query=g.db_session.query(Place).filter(Place.place_number==int(place_number)).first()
            query.car_number=car_number
            query.car_state=1 #车辆状态正常
            g.db_session.commit()
            return 0
        except:
            return -1

    #车辆出场移除该车位车辆信息
    @staticmethod
    def modify_place_number_None(place_number):
        try:
            query=g.db_session.query(Place).filter(Place.place_number==int(place_number)).first()
            query.car_number=None
            query.car_state=None
            g.db_session.commit()
            return 0
        except:
            return -1


    #增加车位
    @staticmethod
    def add_place_number(num):
        try:
            query = g.db_session.query(Place).order_by(Place.place_id.desc()).first()
            place_number_start = 0 if query is None else query.place_number
            for i in range(int(num)):
                place_number = place_number_start + i + 1
                place = Place(place_number=place_number, car_number=None, car_state=None, place_state='True')
                g.db_session.add(place)
            g.db_session.commit()
            return 0
        except:
            return -1

    #修改车位是否开放
    @staticmethod
    def modify_place_state(place_id):
        try:
            query = g.db_session.query(Place).filter(Place.place_id == int(place_id)).first()
            if query:
                if query.car_number is None: #该车位没有车辆停留
                    if query.place_state=='False':  #
                        query.place_state='True'
                        g.db_session.commit()
                        return 1
                    else:
                        query.place_state = 'False'
                        g.db_session.commit()
                        return 0
                else:
                    return -2
            return -1
        except:
            return -1


    #修改车辆状态
    @staticmethod
    def modify_car_state(car_number):
        try:
            query = g.db_session.query(Place).filter(Place.car_number==car_number).first()
            if query:
                if query.car_state == 0:  #0标识车辆故障
                    query.car_state = 1
                    g.db_session.commit()
                    return 1
                else:
                    query.car_state = 0
                    g.db_session.commit()
                    return 0
            return -1
        except:
            return -1


    #分页获取车位列表
    @staticmethod
    def query_place_page(page):
        per_page = 8  # 每页返回的数据量
        start_index = (int(page) - 1) * per_page  # 计算每页数据的起始索引
        query = g.db_session.query(Place)
        # 查询满足条件的记录总数
        total_count = query.count()
        # 计算总页数
        total_pages = (total_count + per_page - 1) // per_page
        # 分页查询并逆向排序
        results = query.order_by(Place.place_id.desc()).limit(per_page).offset(start_index).all()
        return results, total_pages

    # 根据车牌号模糊查询车位信息
    @staticmethod
    def place_car_number_search(car_number):
        return g.db_session.query(Place).filter(Place.car_number.like('%' + car_number + '%')).order_by(
            Place.place_id.desc()).all()

    # 根据车位标识查询车位信息
    @staticmethod
    def place_number_search(place_number):
        return g.db_session.query(Place).filter(Place.place_number==int(place_number)).all()

    #删除车位
    @staticmethod
    def del_place(place_id):
        try:
            query = g.db_session.query(Place).filter(Place.place_id==int(place_id)).first()
            if query.car_number is None: #车位没车的情况下
                g.db_session.delete(query)
                g.db_session.commit()
                return 0
            else:
                return -2 #警告
        except:
            return -1


    # 根据车牌号查询车位信息
    @staticmethod
    def query_place_info(car_number):
        query = g.db_session.query(Place).filter(Place.car_number == car_number).first()
        return query
